package com.cloud.feign.test.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.lwj.logger.aop.LogAspect;
import org.lwj.logger.filter.LogFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfig {
    /*
     * LogFilter
     */
    @Bean
    public FilterRegistrationBean<LogFilter> registerAuthFilter() {
        LogFilter logFilter = new LogFilter();
        logFilter.setMaxLength(10000);
        String[] uriPatterns = { "*.do", "*.action" };
        logFilter.setUriPatterns(uriPatterns);
        String[] excludeUriPatterns = { "*.html", "*.js" };
        logFilter.setExcludeUriPatterns(excludeUriPatterns);
        //
        FilterRegistrationBean<LogFilter> registration = new FilterRegistrationBean<LogFilter>();
        registration.setFilter(logFilter);
        registration.addUrlPatterns("/*");
        registration.setName("logFilter");
        // 值越小，Filter越靠前。
        registration.setOrder(1);
        return registration;
    }
    /*
     * 切面配置
     */
    @Aspect
    private class LogAspectConfig extends LogAspect {

        @Around("@annotation(org.lwj.logger.api.LogMethod)")
        public Object logAround(ProceedingJoinPoint pjp) throws Throwable {
            return super.logAround(pjp);
        }
    }
    /*
     * 日志切面
     */
    @Bean
    public LogAspectConfig logAspectConfig() {
        return new LogAspectConfig();
    }

}
